package oa.base.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 文件读取
 * @author lilang
 *
 */
public class FileRead {

	
	
	/**
	 * 查询不匹配regx 的文件内容
	 * @param file
	 * @param regx
	 */
	public static String readNoRegx(File file,String regx){
		FileReader fileReader;
		String name = null;
		try {
			fileReader = new FileReader(file);
			BufferedReader reader = new BufferedReader(fileReader);
			boolean isHave = false;
			String msg = null;
			while((msg=reader.readLine())!=null){
				if(msg.indexOf(regx)!=-1){
					isHave = true;
					break;
				}
			}
			if(!isHave){
				name = file.getName();
			}
			reader.close();
			fileReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return name;
	} 
	
	
	/**
	 * 读取配置文件
	 * @param path
	 * @param code
	 * @return
	 */
	public static Properties getProperties(String path, String code) {
		Properties con = new Properties();
		InputStreamReader read;
		try {
			// 读取的格式UTF-8
			read = new InputStreamReader(new FileInputStream(path), code);
			con.load(read);
			read.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 读取Txt配置文件
	 * @param path
	 * @param code
	 * @return
	 */
	public static List<String> getTxt(String path, String code) {
		List<String> list = new ArrayList<String>();
		File file = new File(path);
		try {
			// 读取的格式UTF-8
			InputStreamReader read = new InputStreamReader(new FileInputStream(file), code);
			BufferedReader reader = new BufferedReader(read);
			String msg = null;
			while((msg=reader.readLine())!=null){
				list.add(msg);
			}
			reader.close();
			read.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
}
